<template>
  <div class="result-box">
    <!-- 导航栏 -->
    <van-nav-bar
      :title="this.$route.query.key + '的搜索结果'"
      left-arrow
      @click-left="$router.back()"
    />
    <!-- list -->
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <van-cell
        v-for="(item, index) in list"
        :key="index"
        :title="item.title"
        @click="toArtDT(item.art_id)"
      />
    </van-list>
  </div>
</template>

<script>
import { searchartAPI } from "@/api";
export default {
  data() {
    return {
      list: [],
      loading: false,
      finished: false,
      page: 1,
    };
  },
  methods: {
    async onLoad() {
      //  发起请求获取文章数据
      let res = await searchartAPI({
        q: this.$route.query.key,
        page: this.page,
      });
      this.list.push(...res.data.data.results);
      //   把loading设置为false方便下次的加载
      this.loading = false;
      //   判断是否加载完毕加载完毕把 finished变为true,没有加载完就让页数+1
      if (this.list.length == res.data.data.total_count) {
        this.finished = true;
      } else {
        this.page++;
      }
    },
    // 跳转到详情页
    toArtDT(id) {
      this.$router.push({
        name: "details",
        query: {
          id,
        },
      });
    },
  },
};
</script>

<style lang='less' scoped>
.result-box {
  .van-list {
    position: fixed;
    top: 46px;
    bottom: 0;
    overflow: auto;
    width: 100vw;
  }
}
</style>